const { UUIDV4, UUID, STRING, INTEGER, TINYINT, VIRTUAL, Model } = require('sequelize');
const db = require('../../helpers/db');
const GoodsComment = require('./goods-comment');
const GoodsCategory = require('./goods-category');
const Cart = require('../order/cart');
const OrderItem = require('../order/order-item');

class Goods extends Model {}

Goods.init(
    {
        id: {
            type: UUID,
            defaultValue: UUIDV4,
            primaryKey: true,
            comment: '商品ID'
        },
        intro: {
            type: UUID,
            unique: true,
            comment: '外键，商品详情ID'
        },
        categories: {
            type: STRING(128),
            allowNull: false,
            comment: '分类节点路径'
        },
        name: {
            type: STRING(128),
            allowNull: false,
            comment: '商品名'
        },
        tags: {
            type: STRING(64),
            comment: '商品标签'
        },
        cover: {
            type: STRING(128),
            allowNull: false,
            comment: '默认封面图'
        },
        price: {
            type: STRING(32),
            comment: '价格区间'
        },
        marketPrice: {
            type: STRING(32),
            comment: '市场价区间',
            field: 'market_price'
        },
        description: {
            type: STRING(128),
            comment: '描述'
        },
        inventory: {
            type: INTEGER,
            comment: '商品总库存',
            defaultValue: 0
        },
        sales: {
            type: INTEGER,
            comment: '商品总销量',
            defaultValue: 0
        },
        status: {
            type: TINYINT(1),
            comment: '商品状态，0下架，1上架，2已删除',
            defaultValue: 0
        }
    },
    {
        tableName: 'goods',
        underscored: true,
        sequelize: db
    }
);

Goods.hasMany(GoodsComment, {
    foreignKey: 'goods'
});

Goods.hasMany(Cart, {
    foreignKey: 'goods'
});

Goods.hasMany(OrderItem, {
    foreignKey: 'goods'
});

Goods.hasMany(GoodsCategory, {
    foreignKey: 'goods'
});

module.exports = Goods;
